var currLat = new Array();
var currLng = new Array();
var lastLat = new Array();
var lastLng = new Array();
var line = new Array();
var lineSymbol = {};
//FOR OPACACITY
var lineSymbol2= {};
// for color
var lineSymbol3 = {};
// for bad driver;
var lineSymbol4 = {};
var lineCoordinates = {};	
var INIFORDRIVER = true;
var map;
var point = 0;
//DRIVER INFO
var dri_content = new Array;
var dri_id = new Array();
var taxi_number = new Array();
var dri_credit = new Array();
var dri_nickname = new Array;
var badDrivers = Array;
//var contentString = new Array();
//var infowindow = new Array();
//var location1 = new Array();
//var INITIAL = true;
//var tes1 = 0;
//var NUM = 3;
function testCarMoves(map){	
	map = map;	 
	var markers = new Array();
	carMoves_test(lastLat, lastLng, currLat, currLng, map);
	
	//var interv1 = setInterval(updateMarkers, 5000);
}
function carMoves_test(lastLat, lastLng, currLat, currLng, map){
	window.setInterval(function(){
		for (var index in currLat){
			if(INIFORDRIVER){
				lastLat[index] = currLat[index] - 0.002;
				lastLng[index] = currLng[index] - 0.003;
				point++;
				if(point == 8){
					INIFORDRIVER = false;
				}
			}
		else{
			for(var index1 in lastLat){
				lastLat[index1] = currLat[index1];
				lastLng[index1] = currLng[index1];
			}
			point++;
			point = point%4;
			if(point == 0){
				currLat[index] = currLat[index] - Math.random()*0.001;
				currLng[index] = currLng[index] + Math.random()*0.001;
			}else if(point == 1){
				currLat[index] = currLat[index] + Math.random()*0.001;
				currLng[index] = currLng[index] + Math.random()*0.001;
			}else if(point == 2){
				currLat[index] = currLat[index] + Math.random()*0.001;
				currLng[index] = currLng[index] - Math.random()*0.001;
			}else{
				currLat[index] = currLat[index] - Math.random()*0.001;
				currLng[index] = currLng[index] - Math.random()*0.001;
			}
				
			}
		};
//		console.log(lastLat);
//		console.log(lastLng);
//		console.log(currLat);
//		console.log(currLng);
		carMoves(lastLat, lastLng, currLat, currLng, map);
	
	},5000)

}
function carMoves(lastLat, lastLng, currLat, currLng, map){
	for (var index in currLat){
		lineCoordinates[index] = [
 									  new google.maps.LatLng(lastLat[index], lastLng[index]),
		                              new google.maps.LatLng(currLat[index], currLng[index])
		                              ];
		//normal
	    lineSymbol[index] = {
				path : google.maps.SymbolPath.CIRCLE,
				scale: 3,
				strokeColor : 'gray',
				strokeOpacity: 1
		};
	    // temp despear
		lineSymbol2[index] = {
				path : google.maps.SymbolPath.CIRCLE,
				scale: 3,
				strokeColor : 'gray',
				strokeOpacity: 0
		};
		//when notify them
	   lineSymbol3[index] = {
				path : google.maps.SymbolPath.CIRCLE,
				scale: 5,
				strokeColor : 'blue',
				strokeOpacity: 0.5
		}; 
	   //bad taxi!
	   lineSymbol4[index] = {
				path : google.maps.SymbolPath.CIRCLE,
				scale: 5,
				strokeColor : 'blue',
				strokeOpacity: 0.5
		}; 
	   
	   //judge if drivers in notify circle!
	   if(isIn[index]){
			line[index] = new google.maps.Polyline({
				path : lineCoordinates[index],
				icons : [{
					icon : lineSymbol3[index],
					offset : '100%'
				}],
				strokeOpacity: 0.2,
				map : map
			});
	   }else{
		   if(badDrivers[index]){
			line[index] = new google.maps.Polyline({
				path : lineCoordinates[index],
				icons : [{
					icon : lineSymbol4[index],
					offset : '100%'
				}],
				strokeOpacity: 0.2,
				map : map
			});
		   }else{
				line[index] = new google.maps.Polyline({
					path : lineCoordinates[index],
					icons : [{
						icon : lineSymbol[index],
						offset : '100%'
					}],
					strokeOpacity: 0.2,
					map : map
				});
		   }
	   }
	   //ADD INFOWINDOW FOR DRIVER!
		var driLocation = new Array;
		driLocation[index] =  new google.maps.LatLng(currLat[index], currLng[index]);
		var dri_infowidow = new google.maps.InfoWindow({
			content : dri_content[index],
			position : driLocation[index]
		});
		google.maps.event.addListener(line[index], 'click', function(){
			dri_infowidow.open(map)
		});
		animateCircle(index);
	}
}
function changeLineSymble(index){
	var lineSymbol3 = {
			path : google.maps.SymbolPath.CIRCLE,
			scale: 3,
			strokeColor : 'blue',
			strokeOpacity: 0.5
	}; 
	if(line[index]){
		console.log('line exist');
	}
	var  lineIcon = line[index].get('icons');
	lineIcon[0].icon = lineSymbol3;
	line[index].set('icons', lineIcon);
}
function animateCircle(index) {
    var count = new Array;
    count[index] = 0;
	var inver1 = window.setInterval(function() {
	      count[index] = (count[index] + 1) % 200;
	      var icons = line[index].get('icons');
	      icons[0].offset = (count[index] / 2) + '%';
	      line[index].set('icons', icons);

	      //TEMP OPICITY!
	      if(count[index] > 100){
	    	  icons[0].icon = lineSymbol2[index];
	    	  line[index].set('icons', icons);
	  		clearInterval(inver1);
	  	}
	  }, 30);
}
